﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class BaseModuleSettings : ModuleSettingsBase
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        [AccessedThroughProperty("fupSettings")]
        private FileUpload _fupSettings;
        private LinkButton cmdLoad;
        private LinkButton cmdReset;
        private LinkButton cmdSave;

        public BaseModuleSettings()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.fupSettings = new FileUpload();
            this.cmdSave = new LinkButton();
            this.cmdLoad = new LinkButton();
            this.cmdReset = new LinkButton();
        }

        [CompilerGenerated, DebuggerStepThrough]
        private void _Lambda$__3(object a0, EventArgs a1)
        {
            this.cmdSaveClick();
        }

        [CompilerGenerated, DebuggerStepThrough]
        private void _Lambda$__4(object a0, EventArgs a1)
        {
            this.cmdLoadClick();
        }

        [DebuggerStepThrough, CompilerGenerated]
        private void _Lambda$__5(object a0, EventArgs a1)
        {
            this.cmdResetClick();
        }

        private void cmdLoadClick()
        {
            try
            {
                BasePortController controller = new BasePortController();
                if (this.fupSettings.FileName != "")
                {
                    this.fupSettings.SaveAs(this.PortalSettings.HomeDirectoryMapPath + "ImportedSettings.xml");
                    SharedFunctions.DoModuleImport(this.ModuleId, this.PortalSettings.HomeDirectoryMapPath + "ImportedSettings.xml", this.UserId);
                    this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Module", new string[] { "ModuleId=" + Conversions.ToString(this.ModuleId) }));
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdResetClick()
        {
            this.DoReset();
        }

        private void cmdSaveClick()
        {
            BasePortController controller = new BasePortController();
            string fileData = "";
            this.UpdateSettings();
            fileData = ("<content type=\"" + this.ModuleConfiguration.DesktopModule.ModuleName + "\" version=\"" + this.ModuleConfiguration.DesktopModule.Version + "\">") + controller.ExportModule(this.ModuleId) + "</content>";
            SharedFunctions.ForceStringDownload(this.Response, "NB_Store_" + Strings.Replace(this.ModuleConfiguration.DesktopModule.ModuleName, " ", "_", 1, -1, CompareMethod.Binary) + ".xml", fileData);
        }

        public void DoReset()
        {
            try
            {
                SharedFunctions.DoModuleImport(this.ModuleId, this.Server.MapPath(this.ControlPath + @"templates\" + this.ModuleConfiguration.DesktopModule.ModuleName + ".xml"), this.UserId);
                this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Module", new string[] { "ModuleId=" + Conversions.ToString(this.ModuleId) }));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        public void LoadBaseSettings()
        {
            try
            {
                if (!this.Page.IsPostBack)
                {
                    this.LoadCtrls(this);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void LoadCtrls(Control ObjCtrl)
        {
            IEnumerator enumerator;
            try
            {
                enumerator = ObjCtrl.Controls.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
                    if (objectValue is TextBox)
                    {
                        NewLateBinding.LateSet(objectValue, null, "Text", new object[] { Conversions.ToString(this.Settings[NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString()]) }, null, null);
                    }
                    else if (objectValue is CheckBox)
                    {
                        NewLateBinding.LateSet(objectValue, null, "Checked", new object[] { Conversions.ToBoolean(this.Settings[NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString()]) }, null, null);
                    }
                    else
                    {
                        string str;
                        object[] objArray2;
                        bool[] flagArray;
                        if (objectValue is DropDownList)
                        {
                            str = Conversions.ToString(this.Settings[NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString()]);
                            objArray2 = new object[] { str };
                            flagArray = new bool[] { true };
                            if (flagArray[0])
                            {
                                str = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2[0]), typeof(string));
                            }
                            if (NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Items", new object[0], null, null, null), null, "FindByValue", objArray2, null, null, flagArray) != null)
                            {
                                NewLateBinding.LateCall(objectValue, null, "ClearSelection", new object[0], null, null, null, true);
                                object[] arguments = new object[] { str };
                                flagArray = new bool[] { true };
                                if (flagArray[0])
                                {
                                    str = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[0]), typeof(string));
                                }
                                NewLateBinding.LateSetComplex(NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Items", new object[0], null, null, null), null, "FindByValue", arguments, null, null, flagArray), null, "Selected", new object[] { true }, null, null, false, true);
                            }
                        }
                        else if (objectValue is RadioButtonList)
                        {
                            str = Conversions.ToString(this.Settings[NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString()]);
                            objArray2 = new object[] { str };
                            flagArray = new bool[] { true };
                            if (flagArray[0])
                            {
                                str = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2[0]), typeof(string));
                            }
                            if (NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Items", new object[0], null, null, null), null, "FindByValue", objArray2, null, null, flagArray) != null)
                            {
                                NewLateBinding.LateCall(objectValue, null, "ClearSelection", new object[0], null, null, null, true);
                                objArray2 = new object[] { str };
                                flagArray = new bool[] { true };
                                if (flagArray[0])
                                {
                                    str = (string) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2[0]), typeof(string));
                                }
                                NewLateBinding.LateSetComplex(NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Items", new object[0], null, null, null), null, "FindByValue", objArray2, null, null, flagArray), null, "Selected", new object[] { true }, null, null, false, true);
                            }
                        }
                        else if (Operators.ConditionalCompareObjectGreater(NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Controls", new object[0], null, null, null), null, "Count", new object[0], null, null, null), 0, false))
                        {
                            this.LoadCtrls((Control) objectValue);
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.cmdSave.Click += new EventHandler(this._Lambda$__3);
            this.cmdLoad.Click += new EventHandler(this._Lambda$__4);
            this.cmdReset.Click += new EventHandler(this._Lambda$__5);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.cmdSave.Text = DotNetNuke.Services.Localization.Localization.GetString("cmdSave", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal");
            this.cmdLoad.Text = DotNetNuke.Services.Localization.Localization.GetString("cmdLoad", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal");
            this.cmdReset.Text = DotNetNuke.Services.Localization.Localization.GetString("cmdReset", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal");
            this.cmdReset.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdResetMsg", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal") + "');");
            this.cmdLoad.Attributes.Add("onClick", "javascript:return confirm('" + DotNetNuke.Services.Localization.Localization.GetString("cmdLoadMsg", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal") + "');");
            this.cmdSave.CssClass = "NBright_CommandButton";
            this.cmdLoad.CssClass = "NBright_CommandButton";
            this.cmdReset.CssClass = "NBright_CommandButton";
            this.Controls.Add(new LiteralControl("<hr/><div class=\"normalbold\">" + DotNetNuke.Services.Localization.Localization.GetString("settingsheader", "/DesktopModules/NB_Store/App_LocalResources/NB_StoreGlobal") + "</div>"));
            this.Controls.Add(new LiteralControl("<table><tr><td>"));
            this.Controls.Add(this.cmdReset);
            this.Controls.Add(new LiteralControl("</td><td>"));
            this.Controls.Add(this.cmdSave);
            this.Controls.Add(new LiteralControl("</td><td>"));
            this.Controls.Add(this.fupSettings);
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(this.cmdLoad);
            this.Controls.Add(new LiteralControl("</td></tr></table>"));
            this.Controls.Add(new LiteralControl("<br/>TabID : " + this.TabId.ToString()));
        }

        public void UpdateBaseSettings()
        {
            try
            {
                this.UpdateCtrls(this);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void UpdateCtrls(Control ObjCtrl)
        {
            IEnumerator enumerator;
            ModuleController controller = new ModuleController();
            try
            {
                enumerator = ObjCtrl.Controls.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object objectValue = RuntimeHelpers.GetObjectValue(enumerator.Current);
                    if (objectValue is TextBox)
                    {
                        controller.UpdateModuleSetting(this.ModuleId, NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString(), Conversions.ToString(NewLateBinding.LateGet(objectValue, null, "Text", new object[0], null, null, null)));
                    }
                    else if (objectValue is CheckBox)
                    {
                        controller.UpdateModuleSetting(this.ModuleId, NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString(), NewLateBinding.LateGet(objectValue, null, "Checked", new object[0], null, null, null).ToString());
                    }
                    else if (objectValue is DropDownList)
                    {
                        controller.UpdateModuleSetting(this.ModuleId, NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString(), Conversions.ToString(NewLateBinding.LateGet(objectValue, null, "SelectedValue", new object[0], null, null, null)));
                    }
                    else if (objectValue is RadioButtonList)
                    {
                        controller.UpdateModuleSetting(this.ModuleId, NewLateBinding.LateGet(objectValue, null, "ID", new object[0], null, null, null).ToString(), Conversions.ToString(NewLateBinding.LateGet(objectValue, null, "SelectedValue", new object[0], null, null, null)));
                    }
                    else if (Operators.ConditionalCompareObjectGreater(NewLateBinding.LateGet(NewLateBinding.LateGet(objectValue, null, "Controls", new object[0], null, null, null), null, "Count", new object[0], null, null, null), 0, false))
                    {
                        this.UpdateCtrls((Control) objectValue);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        protected virtual FileUpload fupSettings
        {
            [DebuggerNonUserCode]
            get
            {
                return this._fupSettings;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                this._fupSettings = value;
            }
        }
    }
}

